/*
 * @Description: 订单监控类
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-09-09 09:50:11
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2020-01-07 10:43:17
 */
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:C20Driver/model/real_time_order_data.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/utils/flutter_tts.dart';
import 'package:C20Driver/widget/intercity_bus_prompt_box.dart';
import 'package:C20Driver/widget/user_realtime_order.dart';

class OrderMonitoring {
  String voiceTxt;

  // 实时订单检测
  getAppOrderMonitoring(context, onData) {
    var realTimeOrderData = RealTimeOrderData.fromJson(onData);

    // 这里应该是socket连接订单
    if (realTimeOrderData.type == '0020') {
      // 检测到有订单，打开实时单乘客提示框
      _openOrderFrame(context, realTimeOrderData);

      voiceTxt =
          '接到用户实时单, ${realTimeOrderData.depAre}到${realTimeOrderData.destArea}';
    } else if (realTimeOrderData.type == '0022') {
      // 检测到有城际订单
      _openOrderIntercity(context, realTimeOrderData);
      voiceTxt =
          '接到城际订单, ${realTimeOrderData.depAre}到${realTimeOrderData.destArea}';
    } else {
      // 检测到有预约订单
      _openReservationOrder(context);
    }

    /// 语音播报订单
    VoiceBroadCasting.voicePlayback(voiceTxt);
  }

  // 用户实时单
  Future<void> _openOrderFrame(context, realTimeOrderData) async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return UserRealtimeOrder(realTimeOrderData: realTimeOrderData);
      },
    );
  }

  // 城际出行订单
  Future<void> _openOrderIntercity(context, realTimeOrderData) async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return IntercityBusPromptBox(realTimeOrderData: realTimeOrderData);
      },
    );
  }

  // 预约订单
  _openReservationOrder(context) {
    NavigatorUtil.jump(context, '/reservationOrder');
  }
}
